package com.java2007.zhangyu.restaurant.controller.backend;

import com.java2007.zhangyu.restaurant.controller.BaseServlet;
import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.FoodTypeService;
import com.java2007.zhangyu.restaurant.service.impl.FoodTypeServiceImpl;
import com.java2007.zhangyu.restaurant.utils.WebUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 菜类别控制器
 * @author zy
 * @Version
 * @date 2021/01/26
 */
@WebServlet("/foodtype")
public class FoodTypeController extends BaseServlet {

    private FoodTypeService foodTypeService = new FoodTypeServiceImpl();

    /**
     * 根据菜类别 fTypeId 删除
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String del(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数
        String fTypeIdStr = request.getParameter("fTypeId");
        //将字符串类型转换为整型
        int fTypeId = Integer.parseInt(fTypeIdStr);
        //根据菜类别名模糊查询菜类别的信息
        foodTypeService.deleteByFoodTypeId(fTypeId);
        //重定向到查询接口
        return "redirect:/foodtype?method=search";
    }

    /**
     * 回显
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String queryByFtId(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //获取请求参数
        String fTypeIdStr = request.getParameter("fTypeId");
        //将字符串类型转换为整型
        int fTypeId = Integer.parseInt(fTypeIdStr);
        //根据菜类别名id查询菜类别的信息
        FoodType foodType = foodTypeService.queryByFoodTypeId(fTypeId);
        //将查询到的信息放到 request域中
        request.setAttribute("type",foodType);
        //转发
        return "forward:/backend/detail/foodtype/food-type-update.jsp";
    }

    /**
     * 检验添加的菜类别是否存在的方法
     * @param request
     * @param response
     */
    public String checkFoodTypeName(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fTypeName = request.getParameter("fTypeName");
        int result = foodTypeService.checkFoodTypeName(fTypeName);
        return String.valueOf(result);
    }

    /**
     * 添加
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //获取请求参数
        String fTypeName = request.getParameter("fTypeName");
        //根据菜品类别名增加菜品类别
        foodTypeService.saveByFoodTypeName(fTypeName);
        //重定向到查询接口
        return "redirect:/foodtype?method=search";
    }

    /**
     * 搜索
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String search(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数
        String fTypeName = request.getParameter("fTypeName");
        //根据菜类别名模糊查询菜类别的信息
        List<FoodType> types = foodTypeService.queryByFoodTypeName(fTypeName);
        //将查询到的信息放入request域中
        request.setAttribute("types",types);
        request.setAttribute("fTypeName",fTypeName);
        //转发
        return "forward:/backend/detail/foodtype/food-type-list.jsp";
    }

    /**
     * 更新
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //获取请求多个参数，调用工具类 WebUtil
        FoodType foodType = WebUtil.copyParamToBean(request.getParameterMap(), new FoodType());
        //根据菜类别对象更改菜类别的信息
        foodTypeService.updateByFoodTypeName(foodType);
        //重定向到查询接口
        return "redirect:/foodtype?method=search";
    }
}
